package creational.factory;

/**
 * @author hey
 */
public class FactoryDemo{

    public static void main(String[] args) {
        //简单工厂
//        ProductFactory productFactory = new ProductFactory();
//        Product hardware = productFactory.getProduct("hardware");
//        hardware.action();
//
//        Product software = productFactory.getProduct("software");
//        software.action();

        //工厂方法 & 抽象工厂
        Human man= new ManFactory().getHuman();
        man.action();
        Human woman= new WomanFactory().getHuman();
        woman.action();

        Person boy = new ManFactory().getPerson();
        boy.action();
        Person girl = new WomanFactory().getPerson();
        girl.action();
    }
}

/**
 * 简单工厂模式
 */
interface Product{
    public void  action();
}


class Hardware implements Product{

    @Override
    public void action() {
        System.out.println("action----->Hardware");
    }
}


class Software implements Product{

    @Override
    public void action() {
        System.out.println("action----->Software");
    }
}


class ProductFactory{

    public Product getProduct(String productType){
        if ("Hardware".equalsIgnoreCase(productType)){
            return new Hardware();
        }else if ("Software".equalsIgnoreCase(productType)){
            return new Software();
        }else {
            throw new RuntimeException("位置产品类型");
        }
    }
}

/**
 * 工厂方法模式(Factory Method) & 抽象工厂
 */
interface Human{
    public void action();
}


class Man implements Human{

    @Override
    public void action() {
        System.out.println("action----->Man");
    }
}

class Woman implements Human{

    @Override
    public void action() {
        System.out.println("action----->Woman");
    }
}



interface Person{
    public void action();
}

class Boy implements Person{
    @Override
    public void action() {
        System.out.println("action----->Boy");
    }
}

class Girl implements Person{

    @Override
    public void action() {
        System.out.println("action----->Girl");
    }
}


interface HumanFactory{
    public Human getHuman();
    public Person getPerson();
}

class ManFactory implements HumanFactory{

    @Override
    public Human getHuman() {
        return new Man();
    }

    @Override
    public Person getPerson() {
        return new Boy();
    }
}

class WomanFactory implements HumanFactory{
    @Override
    public Human getHuman() {
        return new Woman();
    }

    @Override
    public Person getPerson() {
        return new Girl();
    }
}




